package cn.ainit.sys.pages.config;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.config.TxNamespaceHandler;

import com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod;

import cn.ainit.sys.common.dao.SysBaseDao;
import cn.ainit.sys.menu.SysFunctions;
import cn.ainit.sys.pages.config.entity.ConfigFunctions;
import cn.ainit.sys.pages.config.entity.ConfigObject;
import cn.ainit.sys.query.entity.QueryColumn;

@Transactional
@Repository("pagesDao")
public class PagesConfigDaoImpl extends SysBaseDao implements PagesConfigDao {

	@Override
	public ConfigFunctions getOneConfigFunctions(String funcsId)
			throws Exception {
		try {
			return (ConfigFunctions) this.getSessionFactory().getCurrentSession().get(ConfigFunctions.class, funcsId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ConfigObject getOneConfigObject(String configId) throws Exception {
		try {
		    ConfigObject configObject = (ConfigObject) this.getSessionFactory().getCurrentSession().get(ConfigObject.class, configId);
		    if (configObject != null) {
				return configObject;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<QueryColumn> queryOneColumns(String dsId) throws Exception {
		try {
			String hql = " from QueryColumn q where q.dsid=? and q.firstPageFlag ='T' order by q.orderNum";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, dsId);
			List<QueryColumn> list = query.list();
			if(list != null && list.size() > 0){
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public List<QueryColumn> queryTwoColumns(String dsId) throws Exception {
		try {
			String hql = "from QueryColumn q where q.dsid=? and q.firstPageFlag !='T' order by q.orderNum";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, dsId);
			List<QueryColumn> list = query.list();
			if(list != null && list.size() > 0){
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean saveConfigObject(ConfigObject configObject) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(configObject);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public boolean saveFunc(SysFunctions sysFunctions) throws Exception {
		try {
			sessionFactory.getCurrentSession().save(sysFunctions);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public SysFunctions getOneFunction(String superId) throws Exception {
		try {
			String hql = "from SysFunctions where superFuncId =?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, superId);
			List<SysFunctions> list = query.list();
			if (list !=null & !list.equals("")) {
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
